package day08.synch02;

import day08.synch02._01.CommonUtils;
import day08.synch02._01.Task;
import day08.synch02._01.ThreadA;
import day08.synch02._01.ThreadB;
import day08.synch02._02.CommonUtils01;
import day08.synch02._02.Task01;
import day08.synch02._02.ThreadA01;
import day08.synch02._02.ThreadB01;
import day08.synch02._03.Service;
import day08.synch02._03.Thread1;
import day08.synch02._03.Thread2;
import day08.synch02._04.Biz;
import day08.synch02._04.Th1;
import day08.synch02._04.Th2;
import org.junit.Test;

/**
 * <>
 * <详细描述>
 *
 * @Author WANG.SONG.
 * @Date 2019/3/5 0005 22:41
 * @see
 */
public class Run {


    /**
     *<同步方法synchronized,执行长任务效率低下>
     *@param
     *
     *@return
     *@Author WANG.SONG.
     */
    @Test
    public void run(){
        Task task =new Task();

        ThreadA threadA=new ThreadA(task);
        threadA.start();
        ThreadB threadB=new ThreadB(task);
        threadB.start();
        try {
            System.out.println("当前线程"+Thread.currentThread().getName());
            /**这个sleep作用是让main线程休眠10秒，保证线程A和线程B执行完并计算耗时*/
            Thread.sleep(10000);
        } catch (Exception e) {
            e.printStackTrace();
        }
       long beginTime = CommonUtils.begin1;
       if (CommonUtils.begin2 < CommonUtils.begin1){
           beginTime = CommonUtils.begin2;
       }

        long endTime = CommonUtils.end1;
        if (CommonUtils.end2 > CommonUtils.end1){
            endTime = CommonUtils.end2;
        }

        System.out.println("耗时："+((endTime -beginTime)/1000));

    }

    /**
     *<同步代码块，执行效率比同步方法快>
     *@param
     *
     *@return
     *@Author WANG.SONG.
     */
    @Test
    public void run1(){
        Task01 task =new Task01();

        ThreadA01 threadA=new ThreadA01(task);
        threadA.start();
        ThreadB01 threadB=new ThreadB01(task);
        threadB.start();
        try {
            System.out.println("当前线程"+Thread.currentThread().getName());
            /**这个sleep作用是让main线程休眠10秒，保证线程A和线程B执行完并计算耗时*/
            Thread.sleep(10000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        long beginTime = CommonUtils01.begin1;
        if (CommonUtils01.begin2 < CommonUtils01.begin1){
            beginTime = CommonUtils01.begin2;
        }

        long endTime = CommonUtils01.end1;
        if (CommonUtils01.end2 > CommonUtils01.end1){
            endTime = CommonUtils01.end2;
        }

        System.out.println("耗时："+((endTime -beginTime)/1000));

    }


    /**
     *<使用synchronized(非this对象)同步代码块时，如果对象监视器不是同一个对象，就是异步调用，会交叉运行>
     *Integer 是同一个对象 -128 127 是同步， new String 异步
     *@param
     *
     *@return
     *@Author WANG.SONG.
     */
    @Test
    public void run2(){
        Service service =new Service();
        Thread1 thread1 =new Thread1(service);
        thread1.start();
        Thread2 thread2 =new Thread2(service);
        thread2.start();
        try {
            Thread.sleep(9000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    /**
     *<同步代码块放在非同步方法里进行声明，并不能保证调用方法的线程执行顺序，极易出现脏读>
     *@param
     *
     *@return
     *@Author WANG.SONG.
     */
    @Test
    public void run3(){
        Biz biz =new Biz();
        Th1 th1=new Th1(biz);
        th1.start();
        Th2 th2=new Th2(biz);
        try {
            th2.start();
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


}
